package com.inspur.ssm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.inspur.ssm.pojo.UserBean;
import com.inspur.ssm.service.UserService;

/**
 * 登录控制器
 * @author administrator
 *
 */
@Controller
public class UserController {
	@Autowired
	private UserService loginService;
	
	
	
	  //登录
		@RequestMapping("/login")
		public String manlogin(HttpServletRequest request, UserBean managerinfo) throws Exception{
			UserBean manager = null;
			try{
				//调用service进行用户身份验证
				manager = loginService.findManager(managerinfo);
			}catch(Exception e){
				e.printStackTrace();
				request.setAttribute("message", e.getMessage());
			}
			if(manager!=null){
				HttpSession session = request.getSession();
				//在session中保存用户身份信息
				session.setAttribute("user", manager);
				//重定向到商品列表中
				return "redirect:/toShowNewsList.action";
			}else{
				request.setAttribute("message", "用户名或密码错误！");
			}
			
			return "login";
		}

	@RequestMapping("/")
	public String tologin(HttpSession session)throws Exception {
		// 清除session,直接session过期
		session.invalidate();
		return "login";
	}
}
